package jetpack.com.pzl.architecture;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

//数据持有类
//作为单例数据持有类共享也很有用？？？？ AndroidViewModel
public class UserViewModel extends ViewModel {

    public final MutableLiveData<UserInfo> mUserLiveData = new MutableLiveData<>();

    public UserViewModel() {
        //模拟从网络加载
        mUserLiveData.postValue(new UserInfo(1, "凯特琳"));
    }


    //模拟更新数值
    public void updateData() {
        UserInfo userInfo = mUserLiveData.getValue();
        if (userInfo != null) {
            userInfo.age = 100;
            userInfo.name = "薛之谦";
            mUserLiveData.setValue(userInfo);
        }
    }


    MutableLiveData<UserInfo> getData() {
        return mUserLiveData;
    }

    /**
     *  数据资源回收
     * 所有activity finish 后会回调ViewModel的 onCleared
     */
    @Override
    protected void onCleared() {
        super.onCleared();

        //清理 回收
    }


}
